package de.stefanocke.roo.xtend;

import java.util.Collection;
import java.util.Iterator;

/**
 * 
 * @author Administrator
 * 
 *         Iterable that counts from 0 to size-1.
 */
public class Counter implements Iterable<Integer> {

	private int size;

	public Counter(int size) {
		super();
		this.size = size;
	}

	@Override
	public Iterator<Integer> iterator() {

		return new Iterator<Integer>() {
			int current = 0;

			@Override
			public boolean hasNext() {
				return current < size;
			}

			@Override
			public Integer next() {
				if (current == size) {
					throw new IllegalStateException("No more elements.");
				}
				return current++;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();

			}
		};
	}

	public static Counter counter(int size) {
		return new Counter(size);
	}

	public static Counter counter(Collection<?> c) {
		return new Counter(c.size());
	}

}
